import {
  deleteUserById,
  editUserData,
  newUserData,
  postPageListData,
  postUserListData,
  deletePageById,
  newPageData,
  editPageData
} from '@/service/main/system'
import { defineStore } from 'pinia'
import type { ISystemState } from './type'
import useMainStore from '../main'

const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    userList: [],
    usersTotalCount: 0,

    pageList: [],
    pageTotalCount: 0
  }),

  actions: {
    // 用户的操作
    // this
    async postUserListAction(queryInfo: any) {
      const userListResult = await postUserListData(queryInfo)
      // console.log(userListResult)
      const { totalCount, list } = userListResult.data
      this.userList = list
      this.usersTotalCount = totalCount
      // console.log(this.userList)
    },
    async deleteUserByIdAction(id: number) {
      // 调用接口 删除数据
      // const deleteResult = await deleteUserById(id)
      await deleteUserById(id)
      // console.log(deleteResult)
      // 刷新数据 -重新请求数据
      this.postUserListAction({ offset: 0, size: 10 })
    },
    async newUserDataAction(userInfo: any) {
      const Result = await newUserData(userInfo)
      console.log(Result)
      // 刷新数据 -重新请求数据
      this.postUserListAction({ offset: 0, size: 10 })
    },
    // 编辑
    async editUserDataAction(id: string, userInfo: any) {
      const Result = await editUserData(id, userInfo)
      console.log(Result)
      // 刷新数据 -重新请求数据
      this.postUserListAction({ offset: 0, size: 10 })
    },
    // 针对页面的数据：增删改查
    async postPageListAction(pageName: string, queryInfo: any) {
      const pageListResult = await postPageListData(pageName, queryInfo)
      console.log(pageListResult)

      const { totalCount, list } = pageListResult.data
      // console.log(totalCount, list)
      this.pageList = list
      this.pageTotalCount = totalCount
      const mainStore = useMainStore()
      mainStore.fetchEntireDataAction()
    },
    async deletePageByIdAction(pageName: string, id: number) {
      await deletePageById(pageName, id)
      // console.log(deleteResult)
      this.postPageListAction(pageName, { offset: 0, size: 10 })
      const mainStore = useMainStore()
      mainStore.fetchEntireDataAction()
    },
    async newPageDataAction(pageName: string, pageInfo: any) {
      const Result = await newPageData(pageName, pageInfo)
      console.log(Result)
      // 刷新数据 -重新请求数据
      this.postPageListAction(pageName, { offset: 0, size: 10 })
      // 重新获取一下用户角色列表。。所有数据
      const mainStore = useMainStore()
      mainStore.fetchEntireDataAction()
    },
    // 编辑
    async editPageDataAction(pageName: string, id: string, pageInfo: any) {
      const Result = await editPageData(pageName, id, pageInfo)
      console.log(Result)
      // 刷新数据 -重新请求数据
      this.postPageListAction(pageName, { offset: 0, size: 10 })
      // 重新获取一下用户角色列表。。所有数据
      const mainStore = useMainStore()
      mainStore.fetchEntireDataAction()
    }
  }
})

export default useSystemStore
